package com.sc.nft.dao;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sc.nft.entity.AppVersion;
import com.sc.nft.entity.dto.UpdateAppVersionDTO;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.mapper.AppVersionMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

/**
 * (sc_app_version)数据DAO
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-06-08 09:47:14
 */
@Slf4j
@Repository
public class AppVersionDao extends ServiceImpl<AppVersionMapper, AppVersion> {

    public AppVersion getOneByType(String type) {
        LambdaQueryWrapper<AppVersion> appVersionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        appVersionLambdaQueryWrapper.eq(AppVersion::getType, type);
        return getOne(appVersionLambdaQueryWrapper);
    }

    public Page<AppVersion> query(String type, Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<AppVersion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(type), AppVersion::getType, type);
        return page(new Page<>(pageNo, pageSize), queryWrapper);
    }

    public Boolean saveAppVersion(AppVersion appVersion) {
        return SqlHelper.retBool(baseMapper.insert(appVersion));
    }

    public Boolean updateAppVersion(UpdateAppVersionDTO appVersion) {
        LambdaUpdateWrapper<AppVersion> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(AppVersion::getId, appVersion.getId())
                .set(StrUtil.isNotBlank(appVersion.getName()), AppVersion::getName, appVersion.getName())
                .set(StrUtil.isNotBlank(appVersion.getAppStoreVersion()), AppVersion::getAppStoreVersion, appVersion.getAppStoreVersion())
                .set(StrUtil.isNotBlank(appVersion.getVersion()), AppVersion::getVersion, appVersion.getVersion())
                .set(ObjectUtil.isNotNull(appVersion.getVersionCode()), AppVersion::getVersionCode, appVersion.getVersionCode())
                .set(StrUtil.isNotBlank(appVersion.getAppDesc()), AppVersion::getAppDesc, appVersion.getAppDesc())
                .set(StrUtil.isNotBlank(appVersion.getType()), AppVersion::getType, appVersion.getType())
                .set(StrUtil.isNotBlank(appVersion.getDownloadHref()), AppVersion::getDownloadHref, appVersion.getDownloadHref())
                .set(StrUtil.isNotBlank(appVersion.getTfDownloadHref()), AppVersion::getTfDownloadHref, appVersion.getTfDownloadHref())
                .set(ObjectUtil.isNotNull(appVersion.getIsMust()), AppVersion::getIsMust, appVersion.getIsMust());
        return update(updateWrapper);
    }

    public Boolean deleteAppVersion(Long appVersionId) {
        return SqlHelper.retBool(baseMapper.deleteById(appVersionId));
    }

    public AppVersion findById(Long appVersionId) {
        return baseMapper.selectById(appVersionId);
    }
}